package com.yykj.tencent.im.server.service.impl;

import com.yykj.tencent.im.server.bean.request.*;
import com.yykj.tencent.im.server.bean.response.*;
import com.yykj.tencent.im.server.core.TimUtil;
import com.yykj.tencent.im.server.exception.TimException;
import com.yykj.tencent.im.server.form.AddGroupMemberForm;
import com.yykj.tencent.im.server.form.DeleteGroupMemberForm;
import com.yykj.tencent.im.server.model.entity.GroupProfile;
import com.yykj.tencent.im.server.model.repository.GroupProfileRepository;
import com.yykj.tencent.im.server.service.GroupService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GroupServiceImpl implements GroupService {
    @Value("${environment}")
    private String environment;

    private GroupProfileRepository groupProfileRepository;

    @Autowired
    public void setGroupProfileRepository(GroupProfileRepository groupProfileRepository){
        this.groupProfileRepository = groupProfileRepository;
    }
    /**
     * 版本
     */
    private final String Version = "v4";

    /**
     * 群组管理服务名
     */
    private final String SERVICE_NAME = "group_open_http_svc";

    /**
     * 群组管理相关命令字
     */
    private  final String GET_APPID_GROUP_LIST_COMMAND = "get_appid_group_list";
    private  final String CREATE_GROUP_COMMAND = "create_group";
    private  final String GET_GROUP_INFO_COMMAND = "get_group_info";
    private  final String GET_GROUP_MEMBER_INFO_COMMAND = "get_group_member_info";
    private  final String MODIFY_GROUP_BASE_INFO_COMMAND = "modify_group_base_info";
    private  final String ADD_GROUP_MEMBER_COMMAND = "add_group_member";
    private  final String DELETE_GROUP_MEMBER_COMMAND = "delete_group_member";
    private  final String MODIFY_GROUP_MEMBER_INFO_COMMAND = "modify_group_member_info";
    private  final String DESTROY_GROUP_COMMAND = "destroy_group";
    private  final String GET_JOINED_GROUP_LIST_COMMAND = "get_joined_group_list";
    private  final String GET_ROLE_IN_GROUP_COMMAND = "get_role_in_group";
    private  final String FORBID_SEND_MSG_COMMAND = "forbid_send_msg";
    private  final String GET_GROUP_SHUTTED_UIN_COMMAND = "get_group_shutted_uin";
    private  final String SEND_GROUP_MSG_COMMAND = "send_group_msg";
    private  final String SEND_GROUP_SYSTEM_NOTIFICATION_COMMAND = "send_group_system_notification";
    private  final String CHANGE_GROUP_OWNER_COMMAND = "change_group_owner";
    private  final String GROUP_MSG_RECALL_COMMAND = "group_msg_recall";
    private  final String IMPORT_GROUP_COMMAND = "import_group";
    private  final String IMPORT_GROUP_MSG_COMMAND = "import_group_msg";
    private  final String IMPORT_GROUP_MEMBER_COMMAND = "import_group_member";
    private  final String SET_UNREAD_MSG_NUM_COMMAND = "set_unread_msg_num";
    private  final String DELETE_GROUP_MSG_BY_SENDER_COMMAND = "delete_group_msg_by_sender";
    private  final String GROUP_MSG_GET_SIMPLE_COMMAND = "group_msg_get_simple";
    private  final String GET_ONLINE_MEMBER_NUM_COMMAND = "get_online_member_num";

    /**
     * 请求接口
     */
    private final String api_url = "%s/%s/%s";

    /**
     * 获取 App 中的所有群组
     */
    @SneakyThrows
    @Override
    public GetAppidGroupListResult getAppIdGroupList(GetAppidGroupListRequest getAppidGroupListRequest){
        String api = String.format(api_url, Version, SERVICE_NAME,GET_APPID_GROUP_LIST_COMMAND);
        return TimUtil.post(api,getAppidGroupListRequest,GetAppidGroupListResult.class);
    }

    /**
     * 创建群组
     */
    @SneakyThrows
    @Override
    public CreateGroupResult createGroup(CreateGroupRequest createGroupRequest){
        if(createGroupRequest.getType() == null){
            createGroupRequest.setType("Private");
        }
        String api = String.format(api_url, Version, SERVICE_NAME,CREATE_GROUP_COMMAND);
        CreateGroupResult result = TimUtil.post(api,createGroupRequest,CreateGroupResult.class);
        if(result.getActionStatus().equals("OK")){
            GroupProfile groupProfile = GroupProfile.builder()
                    .groupId(result.getGroupId())
                    .name(createGroupRequest.getName())
                    .ownerAccount(createGroupRequest.getOwnerAccount())
                    .faceUrl(createGroupRequest.getFaceUrl())
                    .environment(environment)
                    .build();
            groupProfileRepository.save(groupProfile);
        }
        return result;
    }

    /**
     * 获取群详细资料
     */
    @SneakyThrows
    @Override
    public GetGroupInfoResult getGroupInfo(GetGroupInfoRequest getGroupInfoRequest){
        String api = String.format(api_url, Version, SERVICE_NAME,GET_GROUP_INFO_COMMAND);
        return TimUtil.post(api,getGroupInfoRequest,GetGroupInfoResult.class);
    }

    /**
     * 获取群成员详细资料
     */
    @SneakyThrows
    @Override
    public GetGroupMemberInfoResult getGroupMemberInfo(GetGroupMemberInfoRequest getGroupMemberInfoRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,GET_GROUP_MEMBER_INFO_COMMAND);
        return TimUtil.post(api,getGroupMemberInfoRequest,GetGroupMemberInfoResult.class);
    }

    /**
     * 修改群基础资料
     */
    @SneakyThrows
    @Override
    public ModifyGroupBaseInfoResult modifyGroupBaseInfo(ModifyGroupBaseInfoRequest modifyGroupBaseInfoRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,MODIFY_GROUP_BASE_INFO_COMMAND);
        return TimUtil.post(api,modifyGroupBaseInfoRequest,ModifyGroupBaseInfoResult.class);
    }

    /**
     * 增加群成员
     */
    @SneakyThrows
    @Override
    public AddGroupMemberResult addGroupMember(AddGroupMemberForm addGroupMemberForm) {
        List<MemberRequestItem> items = new ArrayList<>();
        List<String> members = addGroupMemberForm.getMemberList();
        if(members == null || members.size() == 0){
            throw new TimException("群成员UserID不能为空");
        }
        for(String s:members){
            items.add(new MemberRequestItem(s));
        }
        AddGroupMemberRequest addGroupMemberRequest = AddGroupMemberRequest.builder()
                .groupId(addGroupMemberForm.getGroupId())
                .silence(addGroupMemberForm.getSilence())
                .memberList(items)
                .build();
        String api = String.format(api_url, Version, SERVICE_NAME,ADD_GROUP_MEMBER_COMMAND);
        return TimUtil.post(api,addGroupMemberRequest,AddGroupMemberResult.class);
    }

    /**
     * 删除群成员
     */
    @SneakyThrows
    @Override
    public DeleteGroupMemberResult deleteGroupMember(DeleteGroupMemberForm deleteGroupMemberForm) {
        DeleteGroupMemberRequest request = new DeleteGroupMemberRequest();
        BeanUtils.copyProperties(deleteGroupMemberForm,request);
        request.setMemberToDelAccount(deleteGroupMemberForm.getMemberToDel_Account());
        String api = String.format(api_url, Version, SERVICE_NAME,DELETE_GROUP_MEMBER_COMMAND);
        return TimUtil.post(api,request,DeleteGroupMemberResult.class);
    }

    /**
     * 修改群成员资料
     */
    @SneakyThrows
    @Override
    public ModifyGroupMemberInfoResult modifyGroupMemberInfo(ModifyGroupMemberInfoRequest modifyGroupMemberInfoRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,MODIFY_GROUP_MEMBER_INFO_COMMAND);
        return TimUtil.post(api,modifyGroupMemberInfoRequest,ModifyGroupMemberInfoResult.class);
    }

    /**
     * 解散群组
     */
    @SneakyThrows
    @Override
    public DestroyGroupResult destroyGroup(DestroyGroupRequest destroyGroupRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,DESTROY_GROUP_COMMAND);
        return TimUtil.post(api,destroyGroupRequest,DestroyGroupResult.class);
    }

    /**
     * 获取用户所加入的群组
     */
    @SneakyThrows
    @Override
    public GetJoinGroupListResult getJoinGroupList(GetJoinedGroupListRequest getJoinedGroupListRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,GET_JOINED_GROUP_LIST_COMMAND);
        return TimUtil.post(api,getJoinedGroupListRequest,GetJoinGroupListResult.class);
    }

    /**
     * 查询用户在群组中的身份
     */
    @SneakyThrows
    @Override
    public GetRoleInGroupResult getRoleInGroup(GetRoleInGroupRequest getRoleInGroupRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,GET_ROLE_IN_GROUP_COMMAND);
        return TimUtil.post(api,getRoleInGroupRequest,GetRoleInGroupResult.class);
    }

    /**
     * 批量禁言和取消禁言
     */
    @SneakyThrows
    @Override
    public ForbidSendMsgResult forbidSendMsg(ForbidSendMsgRequest forbidSendMsgRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,FORBID_SEND_MSG_COMMAND);
        return TimUtil.post(api,forbidSendMsgRequest,ForbidSendMsgResult.class);
    }

    /**
     * 获取被禁言群成员列表
     */
    @SneakyThrows
    @Override
    public GetGroupShuttedUinResult getGroupShuttedUin(GetGroupShuttedUinRequest getGroupShuttedUinRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,GET_GROUP_SHUTTED_UIN_COMMAND);
        return TimUtil.post(api,getGroupShuttedUinRequest,GetGroupShuttedUinResult.class);
    }

    /**
     * 在群组中发送普通消息
     */
    @SneakyThrows
    @Override
    public SendGroupMsgResult sendGroupMsg(SendGroupMsgRequest sendGroupMsgRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,SEND_GROUP_MSG_COMMAND);
        return TimUtil.post(api,sendGroupMsgRequest,SendGroupMsgResult.class);
    }

    /**
     * 在群组中发送系统消息
     */
    @SneakyThrows
    @Override
    public SendGroupSystemNotificationResult sendGroupSystemNotification(SendGroupSystemNotificationRequest sendGroupSystemNotificationRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,SEND_GROUP_SYSTEM_NOTIFICATION_COMMAND);
        return TimUtil.post(api,sendGroupSystemNotificationRequest,SendGroupSystemNotificationResult.class);
    }

    /**
     * 转让群主
     */
    @SneakyThrows
    @Override
    public ChangeGroupOwnerResult changeGroupOwner(ChangeGroupOwnerRequest changeGroupOwnerRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,CHANGE_GROUP_OWNER_COMMAND);
        return TimUtil.post(api,changeGroupOwnerRequest,ChangeGroupOwnerResult.class);
    }

    /**
     * 撤回群消息
     */
    @SneakyThrows
    @Override
    public GroupMsgRecallResult groupMsgRecall(GroupMsgRecallRequest groupMsgRecallRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,GROUP_MSG_RECALL_COMMAND);
        return TimUtil.post(api,groupMsgRecallRequest,GroupMsgRecallResult.class);
    }

    /**
     * 导入群基础资料
     */
    @SneakyThrows
    @Override
    public ImportGroupResult importGroup(ImportGroupRequest importGroupRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,IMPORT_GROUP_COMMAND);
        return TimUtil.post(api,importGroupRequest,ImportGroupResult.class);
    }

    /**
     * 导入群消息
     */
    @SneakyThrows
    @Override
    public ImportGroupMsgResult importGroupMsg(ImportGroupMsgRequest importGroupMsgRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,IMPORT_GROUP_MSG_COMMAND);
        return TimUtil.post(api,importGroupMsgRequest,ImportGroupMsgResult.class);
    }

    /**
     * 导入群成员
     */
    @SneakyThrows
    @Override
    public ImportGroupMemberResult importGroupMember(ImportGroupMemberRequest importGroupMemberRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,IMPORT_GROUP_MEMBER_COMMAND);
        return TimUtil.post(api,importGroupMemberRequest,ImportGroupMemberResult.class);
    }

    /**
     * 设置成员未读消息计数
     */
    @SneakyThrows
    @Override
    public SetUnreadMsgNumResult setUnreadMsgNum(SetUnreadMsgNumRequest setUnreadMsgNumRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,SET_UNREAD_MSG_NUM_COMMAND);
        return TimUtil.post(api,setUnreadMsgNumRequest,SetUnreadMsgNumResult.class);
    }


    /**
     * 撤回指定用户发送的消息
     */
    @SneakyThrows
    @Override
    public DeleteGroupMsgBySenderResult deleteGroupMsgBySender(DeleteGroupMsgBySenderRequest deleteGroupMsgBySenderRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,DELETE_GROUP_MSG_BY_SENDER_COMMAND);
        return TimUtil.post(api,deleteGroupMsgBySenderRequest,DeleteGroupMsgBySenderResult.class);
    }

    /**
     * 拉取群成员
     */
    @SneakyThrows
    @Override
    public GroupMsgGetSimpleResult groupMsgGetSimple(GroupMsgGetSimpleRequest groupMsgGetSimpleRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,GROUP_MSG_GET_SIMPLE_COMMAND);
        return TimUtil.post(api,groupMsgGetSimpleRequest,GroupMsgGetSimpleResult.class);
    }

    /**
     * 获取直播群在线人数
     */
    @SneakyThrows
    @Override
    public GetOnlineMemberNumResult getOnlineMemberNum(GetOnlineMemberNumRequest getOnlineMemberNumRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME,GET_ONLINE_MEMBER_NUM_COMMAND);
        return TimUtil.post(api,getOnlineMemberNumRequest,GetOnlineMemberNumResult.class);
    }


}
